//
//  SidebarView.m
//  AmountBody
//
//  Created by smm on 2018/2/2.
//  Copyright © 2018年 RCMTM. All rights reserved.
//
//侧边栏View
#import "SidebarView.h"
#import "EqualCellSpaceFlowLayout.h"
#import "ShoppingCollectionViewCell.h"
#import "ShoppingCollectionReusableView.h"
#import <ReactiveObjC/ReactiveObjC.h>

static const CGFloat HeadView_Tag = 903000;

@implementation SidebarView
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        //创建视图
        [self createSubView];
    }
    return self;
}

#pragma mark -  懒加载
- (NSMutableArray *)c_dataArray;
{
    if (!_c_dataArray) {
        _c_dataArray = [[NSMutableArray array]init];
    }
    return _c_dataArray;
}

-(void)createSubView
{
    CGFloat viewW = SCREEN_W*0.6;
    _screenView = [[UIView alloc]init];
    [self addSubview:_screenView];
    _cancelView = [[UIView alloc]init];
    [self addSubview:_cancelView];
    EqualCellSpaceFlowLayout * layout = [[EqualCellSpaceFlowLayout alloc]initWithType:AlignWithLeft betweenOfCell:5.0];
    // 设置具体属性
    // 1.设置 最小行间距
    layout.minimumLineSpacing = 20;
    // 2.设置 最小列间距
    layout.betweenOfCell  = 15;
    // 设置滑动的方向 (默认是竖着滑动的)
    // 设置item的内边距
    layout.sectionInset = UIEdgeInsetsMake(0,10,10,10);
    //设置collectionView头视图的大小
    [layout setHeaderReferenceSize:CGSizeMake(viewW,40)];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [_screenView addSubview:_collectionView];
    
}

-(void)loadViewForData:(NSMutableArray*)dataArray
{
    self.c_dataArray = dataArray;
    CGFloat viewW = SCREEN_W*0.6;
    CGFloat jianGeW = SCREEN_W-viewW;
    CGFloat viewH = SCREEN_H-TOPNAVIGATIONBAR_H-USERDEFINED_TABBAR_H;
    
    [_cancelView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.width.equalTo(self.mas_width).multipliedBy(0.4);
        
    }];
    _cancelView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelScreen:)];
    [_cancelView addGestureRecognizer:tap];
    [_screenView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right);
        make.top.bottom.equalTo(self);
        make.width.equalTo(@0);
    }];
    [_collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(_screenView);
        make.width.height.equalTo(_screenView);
    }];
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[ShoppingCollectionViewCell class] forCellWithReuseIdentifier:@"ShoppingCollectionViewCell"];
    [_collectionView registerClass:[ShoppingCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ShoppingCollectionReusableView"];
}
#pragma mark - SidebarView的显示与隐藏
-(void)showSidebarView
{
    [_screenView.superview layoutIfNeeded];
    [UIView animateWithDuration:0.37 animations:^{
        [_screenView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self);
            make.top.bottom.equalTo(self);
            make.width.equalTo(self.mas_width).multipliedBy(0.6);
        }];
        [_screenView.superview layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
    }];
    [_collectionView reloadData];
}
-(void)hiddenSidebarView
{
    [_screenView.superview layoutIfNeeded];
    [UIView animateWithDuration:0.37 animations:^{
        [_screenView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self);
            make.top.bottom.equalTo(self);
            make.width.equalTo(@0);
        }];
        [_screenView.superview layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    ShoppingCollectionReusableView *headerView = [_collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ShoppingCollectionReusableView" forIndexPath:indexPath];
    headerView.tag = HeadView_Tag + indexPath.section;
    for (UIView * view in headerView.subviews) {
        [view removeFromSuperview];
    }
    CGFloat jianGeW = 15;
    CGFloat jianGeH = 15;
    CGFloat viewH = 50;
    CGFloat viewW = SCREEN_W/10*6;
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(jianGeW, jianGeH, SCREEN_W-jianGeW*2, Nomal_Label_H)];
    ShoppingCollectionModel *model = [_c_dataArray objectAtIndex:indexPath.section];
    label.text = model.title;
    label.textColor = ZhongZi_Color;
    label.font = Cell_Big_Font;
    [label sizeToFit];
    label.frame = CGRectMake(jianGeW, jianGeH, label.frame.size.width, Nomal_Label_H);
    UIImageView * l_image = [[UIImageView alloc]initWithFrame:CGRectMake(label.frame.size.width+label.frame.origin.x+5, (viewH-1)/2.0, viewW-(label.frame.size.width+label.frame.origin.x+5)-jianGeW, 1)];
    l_image.image = [UIImage imageNamed:@"s_line"];
    
    [headerView addSubview:l_image];
    [headerView addSubview:label];
    
    return headerView;
}
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGFloat viewW = SCREEN_W/10.0*6;
    CGSize size = {viewW, 50};
    return size;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return _c_dataArray.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    ShoppingCollectionModel * model= [_c_dataArray objectAtIndex:section];
    return model.childArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    // 需要配置的代码
    //    [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    ShoppingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShoppingCollectionViewCell" forIndexPath:indexPath];
    ShoppingCollectionModel * tmpModel = [_c_dataArray objectAtIndex:indexPath.section];
    ShoppingCollectionModel *model = [tmpModel.childArr objectAtIndex:indexPath.row];
    RAC(cell,isChoose) = [RACObserve(model, is_Choose) takeUntil:[cell rac_prepareForReuseSignal]];
    
    if (indexPath.section == _c_dataArray.count-1) {
        cell.isTag = YES;
    }else{
        cell.isTag = NO;
        
    }
    [cell setCellWithModel:model];
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShoppingCollectionModel *model = [self.c_dataArray objectAtIndex:[indexPath section]];
    ShoppingCollectionModel *tmpModel = [model.childArr objectAtIndex:[indexPath row]];
    return tmpModel.size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    int type = 0;
    ShoppingCollectionModel * tmpModel = [_c_dataArray objectAtIndex:indexPath.section];
    //    ShoppingCollectionModel *model = [tmpModel.childArr objectAtIndex:indexPath.row];
    [tmpModel.childArr enumerateObjectsUsingBlock:^(ShoppingCollectionModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.is_Choose) {
            obj.is_Choose = NO;
        }
        
    }];
    ShoppingCollectionModel * model = [tmpModel.childArr objectAtIndex:indexPath.row];
    model.is_Choose = !model.is_Choose;
    
}

#pragma mark -  手势按钮方法
-(void)cancelScreen:(UITapGestureRecognizer*)tap
{
    if (_cancelBlock) {
        _cancelBlock();
    }
}

#pragma mark -  Block
static CancelViewBlock _cancelBlock;
+(void)blockForCancelView:(CancelViewBlock)cancelBlock
{
    _cancelBlock = cancelBlock;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
